import { ValidationPipe } from '@nestjs/common';
import { HttpStatus } from '@nestjs/common/enums';
import { HttpException } from '@nestjs/common/exceptions';
import { ValidationError } from 'class-validator';

export default class Validate extends ValidationPipe {
  protected flattenValidationErrors(
    validationErrors: ValidationError[],
  ): string[] {
    const message = {}
    validationErrors.forEach(err => {
      message[err.property] = Object.values(err.constraints)[0]
    })

    throw new HttpException(
      {
        statusCode: 422,
        message
      },
      HttpStatus.UNPROCESSABLE_ENTITY
    )
  }
}